package com.example.administrator.chinafood;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.chinafood.Adapter.MyListViewAdapter;
import com.example.administrator.chinafood.Adapter.PersonRbMyFoodListView;

import java.util.ArrayList;
import java.util.List;

public class PersonRbMyFood extends AppCompatActivity {

    private ListView listView;
    private List<PersonRbMyFoodListView> mdata;
    private MyListViewAdapter<PersonRbMyFoodListView> myListViewAdapter;
    private Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.person_rb_myfood);

        context = PersonRbMyFood.this;

        listView = (ListView)findViewById(R.id.person_rb_myfood_listView);
//        listView.setEmptyView(findViewById(R.id.person_rb_myfood_listView_txt));
        init();
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(context,"点击"+position,Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void init(){
        mdata = new ArrayList<PersonRbMyFoodListView>();

        mdata.add(new PersonRbMyFoodListView("老干妈老坛酸菜牛肉面",1));
        mdata.add(new PersonRbMyFoodListView("风味秘制辣子鸡",2));
        mdata.add(new PersonRbMyFoodListView("夏日清凉汤",3));
        mdata.add(new PersonRbMyFoodListView("小葱拌豆腐",4));
        mdata.add(new PersonRbMyFoodListView("嘻嘻哈哈肉",5));
        mdata.add(new PersonRbMyFoodListView("不知道怎么编了",6));

        myListViewAdapter = new MyListViewAdapter<PersonRbMyFoodListView>((ArrayList)mdata,R.layout.adapter_listview_person_rb_myfood) {
            @Override
            public void bindView(final ViewHolder holder, PersonRbMyFoodListView obj) {
                holder.setText(R.id.person_rb_myfood_listView_txt,obj.getTxt());
                holder.setOnClickListener(R.id.person_rb_myfood_listView_update, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(context,"点击修改按钮",Toast.LENGTH_SHORT).show();
                    }
                });
                holder.setOnClickListener(R.id.person_rb_myfood_listView_delete, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mdata.remove(holder.getItemPosition());
                        myListViewAdapter.notifyDataSetChanged();
                    }
                });
            }
        };
        listView.setAdapter(myListViewAdapter);
    }
}
